﻿/****************************************************************
 * Copyright (c) 2010-2013 Besture All Rights Reserved.
 * 
 * File Name:		SortService 
 * 
 * Created:		2013-8-30 11:47:43
 * Original Author:	nitao 
 * 
 * ------------------------------------------------------------
 * MODIFICATION HISTORY
 * ------------------------------------------------------------
 * When			Who		Why
 * ------------------------------------------------------------
 * 
 *
 ****************************************************************/

using System;
using System.Data;

namespace Besture.Bll
{
    public class SortService
    {
        /// <summary>
        /// 获取可用排序编号
        /// </summary>
        /// <param name="table">表名</param>
        /// <param name="EditPage">编辑页面标识 </param>
        /// <returns></returns>
        public static string GetSortID(string table,bool EditPage)
        {
            if (EditPage) return "";
            string sortID = "1";
            try
            {
                string sql = string.Format(@" SELECT TOP 1 (SortID+1) AS SortID FROM {0} ORDER BY SortID DESC", table);
                DataTable dataTable =  QueryDac.GetTable(sql);
                if(dataTable.Rows.Count>0)
                {
                    sortID = dataTable.Rows[0]["SortID"].ToString();
                    if(string.IsNullOrEmpty(sortID))
                    {
                        sortID = "1";
                    }
                }
            }
            catch (Exception )
            {
                Console.WriteLine();
            }
            return sortID;
        }

        /// <summary>
        /// 获取可用排序编号
        /// </summary>
        /// <param name="table">表名</param>
        /// <param name="EditPage">编辑页面标识 </param>
        /// <returns></returns>
        public static string GetSortIDWeChat(string table, bool EditPage)
        {
            if (EditPage) return "";
            string sortID = "1";
            try
            {
                string sql = string.Format(@" SELECT TOP 1 (ExtColumn6+1) AS ExtColumn6 FROM {0} ORDER BY ExtColumn6 DESC", table);
                DataTable dataTable = QueryDac.GetTable(sql);
                if (dataTable.Rows.Count > 0)
                {
                    sortID = dataTable.Rows[0]["ExtColumn6"].ToString();
                    if (string.IsNullOrEmpty(sortID))
                    {
                        sortID = "1";
                    }
                }
            }
            catch (Exception)
            {
                Console.WriteLine();
            }
            return sortID;
        }
        /// <summary>
        /// 获取可用排序编号
        /// </summary>
        /// <param name="table">表名</param>
        /// <param name="EditPage">编辑页面标识 </param>
        /// <returns></returns>
        public static string GetSortID(string table, bool EditPage,int Type)
        {
            if (EditPage) return "";
            string sortID = "1";
            try
            {
                string sql = string.Format(@" SELECT COUNT(id) AS SortID FROM {0} t WHERE 
t.[Type]={1}", table, Type);
                DataTable dataTable = QueryDac.GetTable(sql);
                if (dataTable.Rows.Count > 0)
                {
                    sortID = dataTable.Rows[0]["SortID"].ToString();
                    if (string.IsNullOrEmpty(sortID))
                    {
                        sortID = "1";
                    }
                }
            }
            catch (Exception)
            {
                Console.WriteLine();
            }
            return sortID;
        }
    }
}
